<%-- 
    Document   : index
    Created on : Mar 24, 2014, 7:09:17 PM
    Author     : asmaa
--%>


        <%@ include file="header.jsp" %>
        <script>
            function validateForm()
            {
                var name = document.forms["reg"]["name"].value;
                var regexName = /^[a-zA-Z\s]+$/;
                if (!regexName.test(name))
                {
                    alert("Name should not contain special character");
                    document.reg.name.focus();
                    return false;
                }
                var job = document.forms["reg"]["job"].value;
                var regexJob = /^[a-zA-Z\s]+$/;
                if (!regexJob.test(job))
                {
                    alert("Job should not contain any numbers");
                    document.reg.job.focus();
                    return false;
                }
                var interest = document.forms["reg"]["interest"].value;
                var regexInt = /^[a-zA-Z\s\,]+$/;
                if (!regexInt.test(interest))
                {
                    alert("Interest should not contain any numbers");
                    document.reg.interest.focus();
                    return false;
                }
                var address = document.forms["reg"]["address"].value;
                var regexAdd = /^[a-zA-Z0-9\s\,]+$/;
                if (!regexAdd.test(address))
                {
                    alert("Address should not contain any special characters");
                    document.reg.address.focus();
                    return false;
                }
                
                var fuData = document.getElementById('image');
                var FileUploadPath = fuData.value;
                if (FileUploadPath != '') {
                    var Extension = FileUploadPath.substring(FileUploadPath.lastIndexOf('.') + 1).toLowerCase();
                    //alert(Extension);
                    if (Extension != "gif" && Extension != "png" && Extension != "bmp" && Extension != "jpeg" && Extension != "jpg") {
                        alert("Photo only allows file types of GIF, PNG, JPG, JPEG and BMP. ");
                        document.reg.image.focus();
                        return false;
                    }
                }

                var pass = document.forms["reg"]["pass"].value;
                //alert("dd");
                var p = pass.length;
                //alert(p);
                if (pass == "" || pass.length < 6 || pass.length > 10)
                {
                    alert("Password must contain at least six characters and max 10");
                    document.reg.pass.focus();
                    return false;
                }
                var credit = document.forms["reg"]["credit"].value;
                var regexCredit = /^[0-9]{16}$/;
                if (!regexCredit.test(credit))
                {
                    alert("Credit Card should contain 16 number");
                    document.reg.credit.focus();
                    return false;
                }
                var creditLimit = document.forms["reg"]["creditLimit"].value;
                var regexCreditLim = /^[0-9]{3,}$/;
                if (!regexCreditLim.test(creditLimit))
                {
                    alert("Credit Limit should be at least 3 digits");
                    document.reg.creditLimit.focus();
                    return false;
                }
                //var email = document.forms["reg"]["email"].value;
                //var atpos = email.indexOf("@");
                //var dotpos = email.lastIndexOf(".");
                
                /* var bdate = document.forms["reg"]["bdate"].value;
                var dateformat = /^(0?[1-9]|[12][0-9]|3[01])[\-](0?[1-9]|1[012])[\-]\d{4}$/;
                var year = bdate.substring(bdate.lastIndexOf('-') + 1).toLowerCase();
                //alert(year);
                //alert("Year");
                if (!dateformat.test(bdate) || year > "1997")
                {
                    alert("Year of birth should be at least 1997 also format is dd-mm-yyyy");
                    document.reg.bdate.focus();
                    return false;
                }*/
                /*if(regexName.test(name) && regexJob.test(job) && regexInt.test(interest)
                 &&  regexAdd.test(address) && regexEmail.test(email) &&Extension == "gif" || Extension == "png" || 
                 Extension == "bmp" || Extension == "jpeg" || Extension == "jpg" && pass != "" && pass.length >= 6 &&
                 pass.length <= 10 && regexCredit.test(credit) && regexCreditLim.test(creditLimit)
                 && dateformat.test(bdate)&& year <"1997")
                 {
                 
                 
                 }*/
                 return false;
            }
            function chkemail()
            {
                var email = document.forms["reg"]["email"].value;
                //var atpos = email.indexOf("@");
                //var dotpos = email.lastIndexOf(".");
                var regexEmail = /^\w+@[a-zA-Z_\.]+?\.[a-zA-Z]{2,3}$/;
                if (!regexEmail.test(email))
                {
                    alert("Not a valid e-mail address");
                    document.reg.email.focus();
                    return false;
                }
                var x;
                if(regexEmail.test(email))
                {
                    var reg = new XMLHttpRequest();
                    reg.open("GET", "CheckMail?email="+email, true);
                    
                    reg.send(null);
                    reg.onreadystatechange = function() {
                        //alert("inside onreadystatechange");
                        if (reg.readyState == 4 && reg.status == 200) {
                            //alert(reg.responseText);
                            if (reg.responseText== 1 || reg.responseText=="1")
                            {
                                alert("Email Should be unique");
                                document.reg.email.focus();
                                x=0;                                                                
                                //alert("x"+x);
                                //alert("x"+x);
                                return false;                                
                            }
                            else
                            {
                                //alert("x"+x);
                                x=1;
                            }
                        }
                                                   //return false;
                    }
                   // alert(x);
                    
                    //return false;
                    
                }
            }
            function chk()
            {
                if(validateForm())
                {
                    return true;
                }
            }
        </script>
        <div class="top_bg">
            <div class="wrap">
                <div class="main_top">
                    <h2 class="style">Registration</h2> 
                </div>
            </div>
        </div>
        <div class="container">
            <div class="row">
                <div class="col-md-6" style="margin-left: 60px;margin-top: 20px">
                    <form action="RegUser" enctype="multipart/form-data" role="form" name="reg"  method="post" >
                        <div class="form-group">
                            <label for="name">Name</label><br>
                            <input class="form-control" type="text" name="name" required placeholder="Full Name"><br>
                        </div>
                        <div class="form-group">
                            <label for="job">Job</label><br>
                            <input class="form-control" type="text" name="job" required placeholder="Job Name"><br>
                        </div>
                        <div class="form-group">
                            <label for="image">Image</label><br>
                            <input type="file" id="image" name="image" required><br>
                        </div>
                        <div class="form-group ">
                            <label for="password">Password</label><br>
                            <input class="form-control" type="password" name="pass" required ><br>
                        </div>
                        <div class="form-group">
                            <label for="email">Email</label><br>
                            <input class="form-control" type="text" name="email" onblur="chkemail()" required placeholder="Valid Email"><br>
                        </div>
                        <div class="form-group">
                            <label for="interest">Interest</label><br>
                            <input class="form-control" type="text" name="interest" required placeholder="Hobbies"><br>
                        </div>
                        <div class="form-group">
                            <label for="address">Address</label><br>
                            <input class="form-control" type="text" name="address" required placeholder="Address"><br>
                        </div>
                        <div class="form-group">
                            <label for="credit">Credit Card</label><br>
                            <input class="form-control" type="text" name="credit" required placeholder="Credit Card Number"><br>
                        </div>
                        <div class="form-group">
                            <label for="creditLimit">Credit Limit</label><br>
                            <input class="form-control" type="text" name="creditLimit" required placeholder="Credit Limit"><br>
                        </div>
                        <div class="form-group">
                            <label for="bdate">Birth Date</label><br>
                            <input class="form-control" type="date" name="bdate" placeholder="dd-mm-yyyy" required><br>
                        </div>
                        <div class="form-group">
                            <button type="submit" class="btn btn-success" style="padding-top: 10px" onclick="return chk()">Register</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
        <%@ include file="footer.jsp" %>
